{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.]\n",
      " [1.]]\n"
     ]
    }
   ],
   "source": [
    "# SVM本质：寻求一个最优的超平面完成分类\n",
    "# 训练基本要求 所有的数据必须有标签\n",
    "# demo：预测身高体重\n",
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "# 准备data\n",
    "dataWomen = np.array([[155,48],[159,50],[164,53],[168,56],[172,60]])\n",
    "dataMen = np.array([[152,53],[156,55],[160,56],[172,64],[176,65]])\n",
    "# 数据合并\n",
    "dataSet = np.vstack((dataWomen,dataMen))\n",
    "dataSet = np.array(dataSet,dtype='float32')\n",
    "# 标签 1 men正样本数据  0 women负样本数据\n",
    "label = np.array([[0],[0],[0],[0],[0],[1],[1],[1],[1],[1]])\n",
    "# 创建SVM\n",
    "svm = cv2.ml.SVM_create()\n",
    "# SVM属性设置\n",
    "svm.setType(cv2.ml.SVM_C_SVC)\n",
    "# 设置内核为线性分类器\n",
    "svm.setKernel(cv2.ml.SVM_LINEAR)\n",
    "svm.setC(0.01)\n",
    "# 训练\n",
    "result = svm.train(dataSet,cv2.ml.ROW_SAMPLE,label)\n",
    "# 预测 第一个样本真实标签为0 第二个样本真实标签为1\n",
    "predict_data = np.vstack([[167,55],[162,57]]) \n",
    "predict_data = np.array(predict_data,dtype='float32')\n",
    "(item,predict) = svm.predict(predict_data)\n",
    "print(predict2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
